# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=osgocean
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-git")
_ver_base=1.0.1
pkgver=1.0.1.r161
pkgrel=1
pkgdesc="An ocean rendering nodekit for OpenSceneGraph (mingw-w64)"
arch=('any')
license=('LGPL')
url="https://github.com/kbale/osgocean/"
depends=("${MINGW_PACKAGE_PREFIX}-fftw" "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph")
makedepends=("git" 'unrar' "${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-cmake")
options=(!strip staticlibs !buildflags)
source=("${_realname}"::"git+https://github.com/kbale/osgocean.git"
        "https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/osgocean/osgOcean-Resources-1.0.1.rar"
        "000-mingw-w64-cmake.patch"
        "001-posix-layout.patch"
        "002-pkgconfig.patch"
        "003-exports.patch")
sha256sums=('SKIP'
            'b3093a48259c3df5af99841934be0468691e506cfbb82eae76eadba4ab71faaf'
            '6d87aae193bbd49c696aa3e78cf8f98c1df6a5459ffd100acb2db748f6f602f7'
            '7aa8298607b760848a30088ca5e8f1df3446c3a690758351d759924342f62c62'
            '7edc108c3a74233d9f3abe0e7022c60de4e8ffc74a7e7b3139a7ce8a09d5bbb5'
            '6a35d93047da6da41adf951d0f992a5f1d532db5c3c4a0d061591780d5604e0e')
noextract=( 'osgOcean-Resources-1.0.1.rar' )

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.r%s" "${_ver_base}" "$(git rev-list --count HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"/resources
  yes | unrar x "${srcdir}/osgOcean-Resources-1.0.1.rar"
  cd ../
  patch -p1 -i ${srcdir}/000-mingw-w64-cmake.patch
  patch -p1 -i ${srcdir}/001-posix-layout.patch
  patch -p1 -i ${srcdir}/002-pkgconfig.patch
  patch -p1 -i ${srcdir}/003-exports.patch
}

build() {
  [[ -d ${srcdir}/build-release ]] && rm -rf ${srcdir}/build-release
  mkdir -p ${srcdir}/build-release && cd ${srcdir}/build-release

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DUSE_FFTW3=ON \
    -DUSE_FFTSS=OFF \
    ../${_realname}
  make

  [[ -d ${srcdir}/build-debug ]] && rm -rf ${srcdir}/build-debug
  mkdir -p ${srcdir}/build-debug && cd ${srcdir}/build-debug

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Debug \
    -DUSE_FFTW3=ON \
    -DUSE_FFTSS=OFF \
    ../${_realname}

  make
}

package_release() {
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-svn")
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-svn)

   cd ${srcdir}/build-release

  make DESTDIR=${pkgdir} install
}

package_debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-git=${pkgver}" "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-svn")
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-debug-svn)

  cd ${srcdir}/build-debug
  make DESTDIR=${pkgdir} install

  rm -rf ${pkgdir}${MINGW_PREFIX}/bin/*.exe
  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/osgOcean
}

package_mingw-w64-i686-osgocean-git() {
  package_release
}

package_mingw-w64-i686-osgocean-debug-git() {
  package_debug
}

package_mingw-w64-x86_64-osgocean-git() {
  package_release
}

package_mingw-w64-x86_64-osgocean-debug-git() {
  package_debug
}
